package io.geekidea.boot.shop.controller.web;

import io.geekidea.boot.auth.annotation.Permission;
import io.geekidea.boot.common.enums.SysLogType;
import io.geekidea.boot.framework.annotation.Log;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.shop.dto.CategoryDto;
import io.geekidea.boot.shop.query.web.CategoryQuery;
import io.geekidea.boot.shop.service.base.api.CategoryService;
import io.geekidea.boot.shop.vo.web.CategoryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 *  控制器
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@RestController
@Tag(name = "")
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加null
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.ADD)
    @Operation(summary = "添加null")
    @PostMapping("/addCategory")
    @Permission("category:add")
    public ApiResult addCategory(@Valid @RequestBody CategoryDto dto) {
        log.info("添加null：{}", dto);
        boolean flag = categoryService.addCategory(dto);
        return ApiResult.result(flag);
    }

    /**
     * 修改null
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.UPDATE)
    @Operation(summary = "修改null")
    @PostMapping("/updateCategory")
    @Permission("category:update")
    public ApiResult updateCategory(@Valid @RequestBody CategoryDto dto) {
        log.info("修改null：{}", dto);
        boolean flag = categoryService.updateCategory(dto);
        return ApiResult.result(flag);
    }

    /**
     * 删除null
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.DELETE)
    @Operation(summary = "删除null")
    @PostMapping("/deleteCategory/{id}")
    @Permission("category:delete")
    public ApiResult deleteCategory(@PathVariable Integer id) {
        log.info("删除null：{}", id);
        boolean flag = categoryService.deleteCategory(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取null详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取null详情")
    @PostMapping("/getCategory/{id}")
    @Permission("category:info")
    public ApiResult<CategoryVo> getCategory(@PathVariable Integer id) {
        log.info("获取null详情：{}", id);
        CategoryVo categoryVo = categoryService.getCategoryById(id);
        return ApiResult.success(categoryVo);
    }

    /**
     * 获取null分页列表
     *
     * @param query
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取null分页列表")
    @PostMapping("/getCategoryPage")
    @Permission("category:page")
    public ApiResult<CategoryVo> getCategoryPage(@Valid @RequestBody CategoryQuery query) {
        log.info("获取null分页列表：{}", query);
        Paging<CategoryVo> paging = categoryService.getCategoryPage(query);
        return ApiResult.success(paging);
    }

}
